package com.elinshaw.pattern.event;

import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
public class AsyncInputEventHandler extends AsyncChannel<InputEvent> {

    private final AsyncEventDispatcher asyncEventDispatcher;

    public AsyncInputEventHandler(AsyncEventDispatcher asyncEventDispatcher) {
        this.asyncEventDispatcher = asyncEventDispatcher;
    }

    @Override
    protected void handle(Event message) {
        InputEvent inputEvent = (InputEvent) message;
        System.out.println(inputEvent.getX() + ":" + inputEvent.getY());
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int result = inputEvent.getX() + inputEvent.getY();
        asyncEventDispatcher.dispatch(new ResultEvent(result));
    }

}
